﻿using System;
using System.Reflection;
using System.Text;
using teavch.Util.Formatter;

namespace teavch.Util
{
	public enum SourceCodeLanguage
	{
		CFormatter,
		CSharpFormatter,
		CssFormatter,
		SqlFormatter,
		VisualBasicFormatter,
		HtmlFormatter,
		jQueryFormatter
	}

	public static class Extensions
	{
		public static string ToString( this string thisString, SourceCodeLanguage sourceLanguage, bool quotedIdenfifiers, bool htmlBased )
		{
			string sSourceCodeLanguageTypeName = Enum.GetName( typeof( SourceCodeLanguage ), sourceLanguage ).ToString();
			return ToString( thisString, sSourceCodeLanguageTypeName, quotedIdenfifiers, htmlBased );
		}

		public static string ToString( this string thisString, string SourceCodeLanguageTypeName , bool quotedIdenfifiers, bool htmlBased )
		{

			Type _type = Type.GetType( "teavch.Util.Formatter.SourceCode." + SourceCodeLanguageTypeName );

			if ( _type == null )
				throw new Exception( "Unable to initialize the formatting engine. Make sure that all required assemblies are in place." );

			MethodInfo methodInfo = _type.GetMethod( "Format", BindingFlags.NonPublic | BindingFlags.Instance );
			
			object result = null;
			
			if ( methodInfo != null )
			{

				ParameterInfo[] paramInfo = methodInfo.GetParameters();
				object instance = Activator.CreateInstance( _type, null );
				if ( paramInfo.Length == 3 )
				{
					object[] param = new object[] { thisString, quotedIdenfifiers, htmlBased };
					result = methodInfo.Invoke( instance, param );
				}
			}
			
			if ( result != null )
				return result.ToString();
			else
				return "";

		}

	}
}
